package com.zhongke.cotrun.controller.manager;

import com.zhongke.cotrun.common.util.APIResponse;
import com.zhongke.cotrun.model.exception.MyException;
import com.zhongke.cotrun.model.pojo.Integral;
import com.zhongke.cotrun.model.resultpackage.CodeMsg;
import com.zhongke.cotrun.service.IntegralService;
import com.zhongke.cotrun.shiro.sys.entity.SysUserEntity;
import com.zhongke.cotrun.shiro.sys.shiro.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

import static com.zhongke.cotrun.common.util.APIResponse.returnSuccess;
@Validated
@RestController
@RequestMapping("/manager/Integral")
public class IntegralController {
    @Autowired
    IntegralService integralService;

    /**
     * 新增积分类型
     *
     * @param integral
     * @return
     */
    @RequestMapping("/addIntegral")
    public APIResponse addIntegral(Integral integral) throws MyException {

        SysUserEntity sysUserEntity = (SysUserEntity) ShiroUtils.getSubject().getPrincipal();
        if (sysUserEntity == null) {
            throw new MyException(CodeMsg.USER_LOGIN_ERROR);
        }
        integral.setCreateId(sysUserEntity.getUserId());
        if (integralService.insertIntegral(integral)) {
            return returnSuccess();
        }
        return APIResponse.returnFail(CodeMsg.ADD_ERROR);
    }

    /**
     * 修改积分类型
     *
     * @param integral
     * @return
     */
    @PostMapping("/updateIntegral")
    public APIResponse updateIntegral(Integral integral) {
        if (integralService.updateIntegral(integral)) {
            return returnSuccess();
        }
        return APIResponse.returnFail(CodeMsg.UPDATE_ERROR);
    }

    /**
     * 根据id查看具体的
     *
     * @param integralId
     * @return
     */
    @GetMapping("/selectIntegralById")
    public APIResponse selectIntegralById(@NotNull(message = "积分类行id不能为空") Integer integralId) {
        return returnSuccess(integralService.selectIntegralByid(integralId));

    }

    /**
     * 根据条件搜索所有的
     *
     * @param integral
     * @param pageNumber
     * @param pageSize
     * @return
     */

    @PostMapping("/selectAllIntegralBycondition")
    public APIResponse selectAllIntegralBycondition(Integral integral, @RequestParam(required = false,defaultValue = "1") Integer pageNumber, @RequestParam(required = false,defaultValue = "10") Integer pageSize) {


        return returnSuccess(integralService.selectIntegralByconditon(integral, pageNumber, pageSize));
    }

    /**
     * 返回积分类型列表
     *
     * @param integral
     * @return
     */
    @PostMapping("/selectAllIntegralList")
    public APIResponse selectAllIntegralList(Integral integral) {
        return APIResponse.returnSuccess(integralService.selectAllIntegral(integral));
    }

    /**
     * 修改积分状态
     * @param state
     * @param integralId
     * @return
     */
    @RequestMapping("/updateIntegralStatue")
  public APIResponse updateIntegralStatue(@NotNull(message = "修改状态不能为空") Integer state,@NotNull(message = "要修改的积分类型id不能为空") Integer integralId) {
        if (integralService.updateIntegralRecord(state, integralId)) {
            return APIResponse.returnSuccess();
        }
        return APIResponse.returnFail(CodeMsg.UPDATE_ERROR);
    }


    /**
     * 后台搜索积分规则列表
     * @param integral
     * @param pageNumber
     * @param pageSize
     * @return
     */

    @PostMapping("/selectIntegralrecordByIntegral")
    public APIResponse selectIntegralrecordByIntegral(Integral integral,@RequestParam(required = false,defaultValue = "1") Integer pageNumber,@RequestParam(required = false,defaultValue = "10") Integer pageSize){


        return APIResponse.returnSuccess(integralService.selectIntegralAndRecord(integral, pageNumber,  pageSize));
    }
}
